package com.tronsis.distribution.biz.impl;

import android.app.Activity;

import com.google.gson.reflect.TypeToken;
import com.tronsis.distribution.bean.UploadFileBean;
import com.tronsis.distribution.biz.FileBiz;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.http.HttpCallBack;
import com.tronsis.library.http.RequestClient;
import com.tronsis.library.http.RequestMethod;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class FileBizImpl implements FileBiz {
    @Override
    public void downLoad(Activity activity, String id, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.FILE_DOWNLOAD + id);
    }

    @Override
    public void thumbnail(Activity activity, int h, String id, Number q, int w, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("h", String.valueOf(h), "q", String.valueOf(q), "w", String.valueOf(w)).excute(RequestMethod.GET, Url.FILE_THUM+id);
    }

    @Override
    public void upload(Activity activity, File file, String type, final UICallBack callBack) throws Exception {
        List<File> fileList = new ArrayList<>();
        fileList.add(file);
        RequestClient.getInstance(new HttpCallBack(activity, UploadFileBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        LogUtil.e("upload", "上传文件成功");
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addFormParameter("file", fileList, "type", type).excute(RequestMethod.POST, Url.FILE_UPLOAD);
    }

    @Override
    public void uploads(Activity activity, ArrayList<File> files, String type, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<List<UploadFileBean>>(){}.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addFormParameter("files", files, "type", type).excute(RequestMethod.POST, Url.FILE_UPLOADS);
    }


}
